package com.pr.makfilm.user.pages.school.list.cmp;

import com.pr.makfilm.core.cmp.table.AjaxDataTable;
import com.pr.makfilm.core.cmp.table.DataProvider;
import com.pr.makfilm.core.cmp.table.SelectionChangeCallback;
import com.pr.makfilm.domain.District;
import com.pr.makfilm.domain.School;
import com.pr.makfilm.domain.SchoolType;
import com.pr.makfilm.domain.filter.OpenSearchDescription;
import com.pr.makfilm.domain.filter.SchoolFilter;
import com.pr.makfilm.security.service.ISecurityService;
import com.pr.makfilm.service.IDistrictService;
import com.pr.makfilm.service.ISchoolService;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 18.09.13
 * Time: 21:40
 * To change this template use File | Settings | File Templates.
 */
public class SchoolTab extends Panel
{
   @SpringBean
   private ISchoolService schoolService;

   @SpringBean
   private ISecurityService securityService;

   private School selected;

   private AjaxDataTable table;

   private SchoolFilterToolbar filterToolbar;

   public SchoolTab(String id)
   {
      super(id);
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      List<IColumn<School, String>> columns = new ArrayList<IColumn<School, String>>();
      columns.add(new PropertyColumn<School, String>(
              new ResourceModel("name"),
              School.FIELD_NAME,
              School.FIELD_NAME
      ));
      columns.add(new PropertyColumn<School, String>(
              new ResourceModel("type"),
              School.FIELD_TYPE,
              School.FIELD_TYPE + "." + SchoolType.FIELD_NAME
      ));
      columns.add(new PropertyColumn<School, String>(
              new ResourceModel("district"),
              School.FIELD_DISTRICT,
              School.FIELD_DISTRICT + "." + District.FIELD_NAME
      ));
      columns.add(new PropertyColumn<School, String>(
              new ResourceModel("city"),
              School.FIELD_CITY,
              School.FIELD_CITY
      ));
      columns.add(new PropertyColumn<School, String>(
              new ResourceModel("street"),
              School.FIELD_STREET,
              School.FIELD_STREET
      ));
      columns.add(new PropertyColumn<School, String>(
              new ResourceModel("zip"),
              School.FIELD_ZIP,
              School.FIELD_ZIP
      ));
      OpenSearchDescription<School> osd = new OpenSearchDescription<School>();
      SchoolFilter filter = new SchoolFilter();
      filter.setUser(securityService.getLoggedInUser());
      osd.setFilter(filter);
      DataProvider dataProvider = new DataProvider(schoolService, osd);
      table = new AjaxDataTable<School>("table", columns, dataProvider);
      table.setSelectionChangeCallback(new SelectionChangeCallback<School>()
      {
         @Override
         public void onSelectionChange(AjaxRequestTarget target, IModel<School> model)
         {
            selected = model.getObject();
            refreshTableView(target);
         }
      });
      table.addFilterToolbar(filterToolbar = new SchoolFilterToolbar(table, new PropertyModel<School>(this, "selected")));
      add(table);
   }

   private void refreshTableView(AjaxRequestTarget target)
   {
      target.add(table);
      target.add(filterToolbar);
   }
}
